package com.sixkey.rabbitmq;

/**
 * ClassName: RabbitReceiver
 * Package: com.sixkey.rabbitmq
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2023/10/15 - 18:42
 * @Version: v1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.sixkey.constant.CommonConstant;
import com.sixkey.constant.RabbitMQConstant;
import com.sixkey.entity.AuthorParticipant;
import com.sixkey.entity.Room;
import com.sixkey.entity.Sign;
import com.sixkey.entity.UserMessage;
import com.sixkey.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 接收MQ发送过来的消息
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RabbitReceiver {

    private final IRoomService roomService;

    private final IAuthorParticipantService authorParticipantService;

    private final ISignService signService;

    private final IUserMessageService userMessageService;

    /**
     * 接收消息会议结束信息同时修改会议室状态为未预约
     * @param
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMQConstant.QUEUE_MEET_ROOM,durable = "true"),
            exchange = @Exchange(value = RabbitMQConstant.EXCHANGE_MEET_ROOM),
            key = {RabbitMQConstant.ROUTING_MEET_ROOM}
    ))
    public void updateRoomStatus(String roomName, Message message, Channel channel) throws IOException {
        if(null != roomName){
            log.info("正在修改会议室状态.............");
            QueryWrapper<Room> wrapper = new QueryWrapper<Room>().eq("name", roomName);
            Room room = roomService.getOne(wrapper);
            room.setStatus(CommonConstant.ROOM_STATUS_NOT_RESERVATION);
            roomService.updateById(room);
            log.info("会议室状态修改成功.............");
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    /**
     * 立即预约后保存数据
     * @param
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMQConstant.QUEUE_RESERVATION,durable = "true"),
            exchange = @Exchange(value = RabbitMQConstant.EXCHANGE_RESERVATION),
            key = {RabbitMQConstant.ROUTING_RESERVATION}
    ))
    public void saveAuthorParticipant(AuthorParticipant authorParticipant, Message message, Channel channel) throws IOException {
        if(null != authorParticipant){
            List<String> participants = authorParticipant.getParticipants();
            participants.stream().forEach(item -> {
                AuthorParticipant aParticipant = new AuthorParticipant();
                aParticipant.setReservationId(authorParticipant.getReservationId());
                aParticipant.setAuthorPhone(authorParticipant.getAuthorPhone());
                aParticipant.setParticipant(item);
                authorParticipantService.save(aParticipant);
            });
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    /**
     * 保存用户签到记录
     * @param
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMQConstant.QUEUE_SIGN,durable = "true"),
            exchange = @Exchange(value = RabbitMQConstant.EXCHANGE_SIGN),
            key = {RabbitMQConstant.ROUTING_SIGN}
    ))
    public void saveSign(Sign sign, Message message, Channel channel) throws IOException {
        if(null != sign){
            log.info("正在保存用户签到记录.............");
            signService.save(sign);
            log.info("用户签到记录保存成功.............");
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    /**
     * 将用户未读的通知做处理
     * @param
     * @param message
     * @param channel
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitMQConstant.QUEUE_BADGE,durable = "true"),
            exchange = @Exchange(value = RabbitMQConstant.EXCHANGE_BADGE),
            key = {RabbitMQConstant.ROUTING_BADGE}
    ))
    public void updateMessage(UserMessage userMessage, Message message, Channel channel) throws IOException {
        if(null != userMessage){
            userMessageService.save(userMessage);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
